package com.ibm.db2oncampus.portal.menu.bean;

import java.io.Serializable;

import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputLink;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

import org.richfaces.component.html.HtmlToolBar;
import org.richfaces.component.html.HtmlToolBarGroup;

import com.ibm.db2oncampus.portal.framework.beans.FacesBeanSupport;
import com.ibm.db2oncampus.portal.menu.domain.ConfiguracaoMenu;
import com.ibm.db2oncampus.portal.menu.service.MenuService;
import com.ibm.db2oncampus.portal.usuario.domain.Person;

/**
 * 
 * @author Wellington
 *
 */
public class MenuRichFacesBean extends FacesBeanSupport implements Serializable {

	private MenuService menuFacade;
	private String iconeModulo;
	private String iconeFuncionalidade;
	HtmlToolBar menuBar;
	
	public MenuRichFacesBean() {

	}

	public HtmlToolBar getMenu() {
		
		
		Person loggedUser = getLoggedUser();
		
		ConfiguracaoMenu config = new ConfiguracaoMenu(iconeModulo, iconeFuncionalidade);
		menuFacade.setConfiguracaoMenu(config);
		
		menuBar = new HtmlToolBar();
		HtmlToolBarGroup menuBarGroup0 = new HtmlToolBarGroup();
		HtmlToolBarGroup menuBarGroup1 = new HtmlToolBarGroup();
		HtmlToolBarGroup menuBarGroup2 = new HtmlToolBarGroup();
		HtmlToolBarGroup menuBarGroup3 = new HtmlToolBarGroup();
		
		menuBar.setWidth("100%");
		menuBar.getChildren().add(menuBarGroup0);
		menuBar.getChildren().add(menuBarGroup1);
		menuBar.getChildren().add(menuBarGroup2);
		menuBar.getChildren().add(menuBarGroup3);
		
        HtmlOutputLink homeLink = new HtmlOutputLink();
        HtmlOutputText homeText = new HtmlOutputText();
        homeLink.setValue((new StringBuilder()).append(((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getContextPath()).append("/view/main.faces").toString());
        homeLink.setStyleClass("menuLinkClass");
        homeText.setValue("Home");
        homeLink.getChildren().add(homeText);		
        menuBarGroup0.getChildren().add(homeLink);
		
		menuBarGroup1.getChildren().addAll(menuFacade.getItensMenuRichFaces(loggedUser));
		
		HtmlOutputText labelUsuario = new HtmlOutputText();
		labelUsuario.setValue("User");
		
		HtmlInputText usuario = new HtmlInputText();
		usuario.setId("userId");
		usuario.setValueBinding("value",
				FacesContext.getCurrentInstance().getApplication().createValueBinding("#{loginBean.userId}"));		
		usuario.setMaxlength(254);
		usuario.setSize(30);
		usuario.setRequired(Boolean.TRUE);
		
		HtmlOutputText labelSenha = new HtmlOutputText();
		labelSenha.setValue("Password");
		
		HtmlInputSecret senha = new HtmlInputSecret();
		senha.setId("password");
		senha.setValueBinding("value",
				FacesContext.getCurrentInstance().getApplication().createValueBinding("#{loginBean.password}"));
		senha.setMaxlength(30);
		senha.setSize(20);
		senha.setRequired(Boolean.TRUE);
		senha.setRedisplay(Boolean.TRUE);
		
		HtmlCommandButton login = new HtmlCommandButton();
		login.setStyle("width: 100px");
		login.setStyleClass("buttons");
		login.setValue("Login");
		login.setAction(
				FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{loginBean.login}", null));
		
		menuBarGroup2.setLocation("right");
		menuBarGroup2.setRendered(getLoggedUser() == null);
		menuBarGroup2.getChildren().add(labelUsuario);
		menuBarGroup2.getChildren().add(usuario);
		menuBarGroup2.getChildren().add(labelSenha);
		menuBarGroup2.getChildren().add(senha);
		menuBarGroup2.getChildren().add(login);

		
		menuBarGroup3.setLocation("right");
		menuBarGroup3.setRendered(getLoggedUser() != null);
		HtmlOutputText usuarioLogado = new HtmlOutputText();
		usuarioLogado.setValueBinding("value",
				FacesContext.getCurrentInstance().getApplication().createValueBinding("#{loggedUser.userId}"));
		
		HtmlOutputText sairText = new HtmlOutputText();
		HtmlOutputLink sairLink = new HtmlOutputLink();
		sairLink.setStyleClass("menuLinkClass");
		sairLink.setValue(
				(new StringBuilder()).append(((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getContextPath()).append("/view/security/logout.faces").toString());
		sairText.setValue("Logout");
        sairLink.getChildren().add(sairText);
		
		menuBarGroup3.getChildren().add(usuarioLogado);
		menuBarGroup3.getChildren().add(sairLink);
		
		return menuBar;
	}
	
	public void setMenu(HtmlToolBar o) {
		System.out.println(o);
	}

	public MenuService getMenuFacade() {
		return menuFacade;
	}

	public void setMenuFacade(MenuService menuFacade) {
		this.menuFacade = menuFacade;
	}

	public String getIconeModulo() {
		return iconeModulo;
	}

	public void setIconeModulo(String iconeModulo) {
		this.iconeModulo = iconeModulo;
	}

	public String getIconeFuncionalidade() {
		return iconeFuncionalidade;
	}

	public void setIconeFuncionalidade(String iconeFuncionalidade) {
		this.iconeFuncionalidade = iconeFuncionalidade;
	}
}